/**
 * 前端sql工具,条件构造
 */
const queryWrapper = {
	_defaultSql: "",
	/**
	 * 注入sql,该值无法直接通过对象属性直接拿出,需要调用下方getSql
	 * @param {Object} sql初始化sql
	 */
	initSql: function(sql) {
		this._defaultSql = sql;
		if(!this.isExistWhere()){
			this._defaultSql+=" where "
		}
	},
	/**
	 * 等于查询
	 * @param {Object} column 列
	 * @param {Object} value 值
	 */
	eq: function(column, value) {
		if (this.isExistWhere()) {
			if (typeof value == 'number') {
				this._defaultSql += column + "=" + value

			} else if (typeof value == 'string') {
				this._defaultSql += column + "='" + value + "'"

			} else {
				this._defaultSql += column + "='" + value + "'"
			}
		} else {
			if (typeof value == 'number') {
				this._defaultSql += " and " + column + "=" + value

			} else if (typeof value == 'string') {
				this._defaultSql += " and " + column + "='" + value + "'"

			} else {
				this._defaultSql += " and " + column + "='" + value + "'"
			}
		}

	},
	/**
	 * 大于查询
	 * @param {Object} column 列
	 * @param {Object} value 值
	 */
	gt:function(column,value){
	if (this.isExistWhere()) {
		if (typeof value == 'number') {
			this._defaultSql += column + ">" + value
	
		} else if (typeof value == 'string') {
			this._defaultSql += column + ">'" + value + "'"
	
		} else {
			this._defaultSql += column + ">'" + value + "'"
		}
	} else {
		if (typeof value == 'number') {
			this._defaultSql += " and " + column + ">" + value
	
		} else if (typeof value == 'string') {
			this._defaultSql += " and " + column + ">'" + value + "'"
	
		} else {
			this._defaultSql += " and " + column + ">'" + value + "'"
		}
	}	
	},
	/**
	 * 大于等于查询
	 * @param {Object} column 列
	 * @param {Object} value 值
	 */
	ge:function(column,value){
	if (this.isExistWhere()) {
		if (typeof value == 'number') {
			this._defaultSql += column + ">=" + value
	
		} else if (typeof value == 'string') {
			this._defaultSql += column + ">='" + value + "'"
	
		} else {
			this._defaultSql += column + ">='" + value + "'"
		}
	} else {
		if (typeof value == 'number') {
			this._defaultSql += " and " + column + ">=" + value
	
		} else if (typeof value == 'string') {
			this._defaultSql += " and " + column + ">='" + value + "'"
	
		} else {
			this._defaultSql += " and " + column + ">='" + value + "'"
		}
	}	
	},
	/**
	 * 小于查询
	 * @param {Object} column 列
	 * @param {Object} value 值
	 */
	lt:function(column,value){
	if (this.isExistWhere()) {
		if (typeof value == 'number') {
			this._defaultSql += column + "<" + value
	
		} else if (typeof value == 'string') {
			this._defaultSql += column + "<'" + value + "'"
	
		} else {
			this._defaultSql += column + "<'" + value + "'"
		}
	} else {
		if (typeof value == 'number') {
			this._defaultSql += " and " + column + "<" + value
	
		} else if (typeof value == 'string') {
			this._defaultSql += " and " + column + "<'" + value + "'"
	
		} else {
			this._defaultSql += " and " + column + "<'" + value + "'"
		}
	}		
	},
	/**
	 * 小于等于查询
	 * @param {Object} column 列
	 * @param {Object} value 值
	 */
	le:function(column,value){
	if (this.isExistWhere()) {
		if (typeof value == 'number') {
			this._defaultSql += column + "<=" + value
	
		} else if (typeof value == 'string') {
			this._defaultSql += column + "<='" + value + "'"
	
		} else {
			this._defaultSql += column + "<='" + value + "'"
		}
	} else {
		if (typeof value == 'number') {
			this._defaultSql += " and " + column + "<=" + value
	
		} else if (typeof value == 'string') {
			this._defaultSql += " and " + column + "<='" + value + "'"
	
		} else {
			this._defaultSql += " and " + column + "<='" + value + "'"
		}
	}		
	},
	/**
	 * like 全模糊查询
	 * @param {Object} column 列
	 * @param {Object} value 值
	 */
	like: function(column, value) {
		if (this.isExistWhere()) {
			this._defaultSql += column + " like '%" + value + "%'"
		} else {
			this._defaultSql += " and " + column + " like '%" + value + "%'"
		}
	},
	/**
	 * 左like 匹配以 xx值结尾的
	 * @param {Object} column 列
	 * @param {Object} value 值
	 */
	leftLike: function(column, value){
		if (this.isExistWhere()) {
			this._defaultSql += column + " like '%" + value + "'"
		} else {
			this._defaultSql += " and " + column + " like '%" + value + "'"
		}
	},
	/**
	 * 右like 匹配以 xx值开头的
	 * @param {Object} column 列
	 * @param {Object} value 值
	 */
	rightLike: function(column, value){
		if (this.isExistWhere()) {
			this._defaultSql += column + " like '" + value + "%'"
		} else {
			this._defaultSql += " and " + column + " like '" + value + "%'"
		}
	},
	/**
	 * 多值 in查询
	 * @param {Object} column 列
	 * @param {Object} values 集合
	 */
	in: function(column,values){
		if (this.isExistWhere()) {
		    let valuesSql="";
		values.forEach((item, index) => {
			if (typeof item == 'number') {
				valuesSql +=  item + "," 
			
			} else if (typeof item == 'string') {
				valuesSql += "'"+item + "'," 
			
			} else {
				valuesSql += "'"+item + "'," 
			}
		})
		this._defaultSql += column + " in (" + valuesSql.substring(0,valuesSql.length-1) + ")"

		} else {
			let valuesSql="";
			values.forEach((item, index) => {
				if (typeof item == 'number') {
					valuesSql +=  item + "," 
				
				} else if (typeof item == 'string') {
					valuesSql += "'"+item + "'," 
				
				} else {
					valuesSql += "'"+item + "'," 
				}
			})
			this._defaultSql += " and "+column + " in (" + valuesSql.substring(0,valuesSql.length-1) + ")"
			
		}
		
	},
	/**
	 * 多值 not in查询
	 * @param {Object} column 列
	 * @param {Object} values 集合
	 */
	notIn: function(column,values){
		if (this.isExistWhere()) {
		    let valuesSql="";
		values.forEach((item, index) => {
			if (typeof item == 'number') {
				valuesSql +=  item + "," 
			
			} else if (typeof item == 'string') {
				valuesSql += "'"+item + "'," 
			
			} else {
				valuesSql += "'"+item + "'," 
			}
		})
		this._defaultSql += column + " not in (" + valuesSql.substring(0,valuesSql.length-1) + ")"
	
		} else {
			let valuesSql="";
			values.forEach((item, index) => {
				if (typeof item == 'number') {
					valuesSql +=  item + "," 
				
				} else if (typeof item == 'string') {
					valuesSql += "'"+item + "'," 
				
				} else {
					valuesSql += "'"+item + "'," 
				}
			})
			this._defaultSql += " and "+column + " not in (" + valuesSql.substring(0,valuesSql.length-1) + ")"
			
		}
		
	},
	/**
	 * 区间查询 ，闭区间
	 * @param {Object} column 列
	 * @param {Object} value1 值1
	 * @param {Object} value2 值2
	 */
	
	betweenAnd:function(column,value1,value2){
		if (this.isExistWhere()) {
	
				this._defaultSql += column + " between " + value1+" and "+value2
		
		
		} else {
	
				this._defaultSql += " and " + column + " between " + value1 +" and "+value2
		
		
		}
	},
	/**
	 * sql最尾部是否存在where,以便拼接sql,防止多个and
	 */
	isExistWhere: function() {
		const where=this._defaultSql.substring(this._defaultSql.length-6);
		if (where=="where "|| where=="WHERE ") {
			return true;
		} else {
			return false;
		}
	},
	/**
	 * 获取生成的sql
	 */
	getSql:function(){
		return this._defaultSql;
	}




}

export default queryWrapper